#include "LgGameMapCollect.h"
#include "LgSpritePropertyGem.h"
#include "LgSpriteGemCarrier.h"
#include "PlayerLg.h"
#include "..\MapViewer\StructureAndConst.h"
LgGameMapCollect::LgGameMapCollect(void):m_nCurrentGem(0)
{
}

LgGameMapCollect::~LgGameMapCollect(void)
{
	SAFE_DELETE(m_vfGemsInterval);
}

bool LgGameMapCollect::GameStart(DWORD dwCurrentTime)
{
	FilterGems();

	unsigned int i=0;
	for (i=0;i<m_vecPlayerLgSprite.size();i++){
		m_vecPlayerLgSprite[i]->SetRevivePos(m_vecPlayerLgSprite[i]->GetPos());
	}
	PlayerLg::SetBreathLevel(NBreathLevel);
	BaseLogicGameMap::GameStart(dwCurrentTime);

	return true;
}
BaseLogicSprite* LgGameMapCollect::CreateGemCarrier(int nCarrierWidth)
{
	m_pGem10Ship=new LgSpriteGemCarrier(20,// fly through screen in 20s
		D3DXVECTOR2(m_nWidth+nCarrierWidth/2.0f,NUFOyPos),
		D3DXVECTOR2(-nCarrierWidth/2.0f,NUFOyPos));
	if (!m_pGem10Ship){
		return NULL;
	}
	return m_pGem10Ship;
}
void LgGameMapCollect::AddSpout(int x,int y)
{
	D3DXVECTOR2 pos((float)x,(float)y);
	m_vecSpoutsPos.push_back(pos);
}
void LgGameMapCollect::FilterGems()
{
	//remove gems 
	vector<LgSpriteProperty*>::iterator it=m_vecProperties.begin();
	while(it!=m_vecProperties.end()){
		if( eGem2==(*it)->GetType()||
			eGem3==(*it)->GetType()||
			eGem5==(*it)->GetType()||
			eGem10==(*it)->GetType()||
			eGemChange==(*it)->GetType()){
			m_vecGems.push_back(dynamic_cast<LgSpritePropertyGem*>(*it));
			it=m_vecProperties.erase(it);
		}else{
			it++;
		}
	}

	//gems
	srand(::GetTickCount());
	m_nGemCount=m_vecGems.size();
	m_vfGemsInterval=new float[m_nGemCount];
	AssignPropertiesTimes(m_vfGemsInterval,m_nGemCount,m_GameTime);
	Disorder<LgSpritePropertyGem>(m_vecGems);
}

void LgGameMapCollect::UpdateAllLgSprite(DWORD dwCurrentTime)
{
	if (m_bGameOver){
		return;
	}

	BaseLogicGameMap::UpdateAllLgSprite(dwCurrentTime);

	//update gems
	if (m_nCurrentGem<m_nGemCount){
		if (m_lLastTime>m_vfGemsInterval[m_nCurrentGem]*1000.0f){
			if(m_vecGems[m_nCurrentGem]->GetType()==eGem10){
				m_nCurrentSpoutIdx=-1;
				m_pGem10Ship->FlyOut(m_vecGems[m_nCurrentGem],
					D3DXVECTOR2((float)(rand()%(m_nWidth+1-40)+20),NUFOyPos));
			}else{
				int nRnd=rand()%m_vecSpoutsPos.size();
				m_vecGems[m_nCurrentGem]->PopUp(m_vecSpoutsPos[nRnd].x,m_vecSpoutsPos[nRnd].y);
				m_nCurrentSpoutIdx=nRnd;
			}
			m_nCurrentGem++;
		}
	}
	//statistic group mark.
	unsigned int i=0;
	unsigned short nGroupMark[6];
	ZeroMemory(nGroupMark,6*sizeof(unsigned short));
	for (i=0;i<m_vecPlayerLgSprite.size();i++){
		if (m_vecPlayerLgSprite[i]->m_nGroup!=NFreeGroup){
			nGroupMark[m_vecPlayerLgSprite[i]->m_nGroup]+=m_vecPlayerLgSprite[i]->m_nGem;
		}
	}
	for (i=0;i<m_vecPlayerLgSprite.size();i++){
		if (m_vecPlayerLgSprite[i]->m_nGroup==NFreeGroup){
			if(m_vecPlayerLgSprite[i]->m_nGem>GetWinGemMark()){
				m_bGameOver=true;
				break;
			}
		}else{
			if(nGroupMark[m_vecPlayerLgSprite[i]->m_nGroup]>GetWinGemMark()){
				m_bGameOver=true;
				break;
			}
		}
	}
	m_pGem10Ship->Update(dwCurrentTime);
}